package com.common.db;

import java.sql.*;
import java.util.*;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

public class DBUtils {

	private static Logger log = Logger.getLogger(DBUtils.class);
	protected static String userName = null; //System.getProperty("WLS_USER");
	protected static String userPassword = null; //System.getProperty("WLS_PW");
	protected static Context ctx = null;

	public DBUtils() {
	}


	/**
	*
	* @throws Exception
	* @return Context
	*/
	private static Context getContext() throws Exception {
		log.debug("getContext():====="+ctx);
		if (ctx == null) {
			log.debug(" ctx null!");
			Properties prop = new Properties();
			prop.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
			prop.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
			prop.put(Context.SECURITY_PRINCIPAL, "weblogic");
			prop.put(Context.SECURITY_CREDENTIALS, "release_shrd_12341");

			//prop.put(Context.INITIAL_CONTEXT_FACTORY, Constants.WL_INIT_FACTORY);
			//prop.put(Context.PROVIDER_URL, Constants.WL_URL);
			//prop.put(Context.SECURITY_PRINCIPAL, Constants.WL_USERNAME);
			//prop.put(Context.SECURITY_CREDENTIALS, Constants.WL_PASSWORD);
//			log.debug("----------------------" + Constants.WL_URL);
//			log.debug("----------------------" + Constants.WL_INIT_FACTORY);
//			log.debug("------------weblogic----------" + Constants.WL_USERNAME);
//			log.debug("---------release_shrd_12341-------" + Constants.WL_PASSWORD);
			ctx = new InitialContext(prop);
		}
		log.debug(ctx + "||" + ctx.getEnvironment());
		return ctx;
	}

	/**
	 *
	 * @throws Exception
	 * @return Connection
	 */
	public static Connection getUserConn() throws Exception {
		DataSource ds = null;
		Connection conn = null;
		try {
			log.debug("Getting JNDI(JNDI= puser  Connection! ");
			ds = (DataSource) getContext().lookup("puser");
			ds.setLoginTimeout(5);
			conn = ds.getConnection();
		}catch (Exception e) {
			log.error("getJNDIConnection() ������Ϣ����:"+e);
		}
		return conn;
	}

	/**
	 *
	 * @throws Exception
	 * @return Connection
	 */
	public static Connection getBillConn() throws Exception {
		DataSource ds = null;
		Connection conn = null;
		try {
			log.debug("Getting JNDI(JNDI= pbill  Connection! ");
			ds = (DataSource) getContext().lookup("pbill");
			ds.setLoginTimeout(5);
			conn = ds.getConnection();
		}catch (Exception e) {
			log.error("getJNDIConnection() ������Ϣ����:"+e);
		}
		return conn;
	}

	/**
	 *
	 * @throws Exception
	 * @return Connection
	 */
	public static Connection getConn() throws Exception {
		DataSource ds = null;
		Connection conn = null;
		try {
			log.debug("Getting JNDI(JNDI= xuka  Connection! ");
			ds = (DataSource) getContext().lookup("xuka");
			ds.setLoginTimeout(5);
			conn = ds.getConnection();
		}catch (Exception e) {
			log.error("getJNDIConnection() ������Ϣ����:"+e);
		}
		return conn;
	}

	/**
	 *
	 * @param jndi String
	 * @throws Exception
	 * @return Connection
	 */
	public static Connection getConnection(String jndi) throws Exception {
		DataSource ds = null;
		Connection conn = null;
		try {
			log.debug("Getting JNDI(JNDI= "+jndi+"  Connection! ");
			ds = (DataSource) getContext().lookup(jndi);
			ds.setLoginTimeout(5);
			conn = ds.getConnection();
		}catch (Exception e) {
			log.error("getJNDIConnection() error:"+e);
		}
		return conn;
	}



}
